package com.ubxtech.wecom.config;

import com.ubxtech.wecom.properties.WecomProperties;
import com.ubxtech.wecom.service.WecomTokenService;
import com.ubxtech.wecom.service.impl.WecomTokenServiceImpl;
import com.ubxtech.wecom.util.WecomMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Victor.Xiao
 * @since 2022-11-16 20:36
 **/
@Configuration
@Slf4j
@EnableConfigurationProperties(WecomProperties.class)
@ConditionalOnProperty(prefix = WecomProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class WecomConfig {

    @Bean
    @ConditionalOnMissingBean
    public WecomTokenService wecomTokenService() {
        return new WecomTokenServiceImpl();
    }


    @Bean
    public WecomMsgUtil wecomMsgUtil(WecomProperties wecomProperties,WecomTokenService wecomTokenService) {
        WecomMsgUtil wecomMsgUtil = WecomMsgUtil.getInstance();
        wecomMsgUtil.setWecomProperties(wecomProperties);
        wecomMsgUtil.setWecomTokenService(wecomTokenService);
        return wecomMsgUtil;
    }

}
